package ee.ttu.lecturerate.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.app.Activity;
import android.os.AsyncTask;

public class RequestAsyncTask extends AsyncTask<RequestInput, Void, ResponseResult>
{
	private static final int CONNECTION_TIMEOUT = 3000;
	private static final int DATA_TIMEOUT = 5000;
	
	private HttpClient client;
	
	private Activity currentActivity;
	private AsyncTaskExecutor executor;
	
	public RequestAsyncTask(Activity activity)
	{
		client = new DefaultHttpClient();
		currentActivity = activity;
		executor = null;
	}
	
	public RequestAsyncTask(AsyncTaskExecutor executor, Activity activity)
	{
		client = new DefaultHttpClient();
		currentActivity = activity;
		this.executor = executor;
	}
	
	
	@Override
	protected ResponseResult doInBackground(RequestInput... inputs)
	{
		RequestInput input = inputs[0];
		ResponseResult result = new ResponseResult();
		result.setType(input.getType());
		
		try
		{
			HttpPost request = new HttpPost(input.getUrl());

			HttpParams params = client.getParams();
			HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
			HttpConnectionParams.setSoTimeout(params, DATA_TIMEOUT);
			request.setParams(params);
			
			request.setEntity(new UrlEncodedFormEntity(input.getParameters()));
			HttpResponse response = client.execute(request);
			
			StatusLine statusLine = response.getStatusLine();
			
			if(statusLine.getStatusCode() == HttpStatus.SC_OK)
			{
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				response.getEntity().writeTo(out);
				out.close();
						
				result.setResponse(out.toString());
			}
			else
			{
				response.getEntity().getContent().close();
				throw new IOException(statusLine.getReasonPhrase());
			}
		}
		catch(UnsupportedEncodingException e) {
			result.setErrorType(1);
			result.setErrorMessage(e.getMessage());
		}
		catch (ClientProtocolException e) {
			result.setErrorType(2);
			result.setErrorMessage(e.getMessage());
		}
		catch (IOException e) {
			result.setErrorType(3);
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}
	
	@Override
	protected void onPostExecute(ResponseResult result)
	{
		super.onPostExecute(result);
		
		if(executor != null) {
			executor.addResult(result);
			
			if(executor.checkAllDone() == true) {
				executor.onPostExecute();
			}
		}
		else
		{
			ResponseManager manager = new ResponseManager(currentActivity);
			manager.manage(result);
		}
	}
	

}
